package com.mapbar.ure.backend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * DFA的数据结构
 * 
 * @author QIN Jian
 * 
 * @param <T>
 */
public class DFA<T> {
	private DFANode<T> root;
	private ArrayList<DFANode<T>> all;

	public void print() {
		System.out.println(all.size());
		int count = 0;
		for (DFANode<T> node : all) {
			System.out.println("=================");
			System.out.println(count);
			node.print(all);
			count++;
		}
	}

	public DFANode<T> getRoot() {
		return root;
	}

	public void setRoot(DFANode<T> root) {
		this.root = root;
	}

	public ArrayList<DFANode<T>> getAll() {
		return all;
	}

	public void setAll(ArrayList<DFANode<T>> all) {
		this.all = all;
	}

	public int match(List<T> input) {
		DFANode<T> p = root;
		for (int i = 0; i < input.size(); ++i) {
			Map<T, DFANode<T>> next = p.getNext();
			T k = input.get(i);
			if (next.containsKey(k)) {
				p = next.get(input.get(i));
			} else {
				return i;
			}
		}
		if (p.isEndState()) {
			return input.size();
		}
		return input.size() - 1;
	}
}
